<script setup>
    import { sdCityList } from '@/utils/index'
    import { getJobLibraryList, jobLibraryDelete, getJobHireList, jobHireDelete } from '@/common/apis'
    import AddPost from './AddPost.vue'
    import AddPost2 from './AddPost2.vue'

    const tabbarData = ['人员管理', '岗位聘用管理']
    let current = $ref(0)
    
    // 人员列表
    let resData = $ref([])

    // 总条数
    let totalCount = $ref(0)

    // 筛选
    let filterParams = $ref({
        pageNo: 1,
        pageSize: 20,
    })

    // 列表数据加载中
    let dataLoading = $ref(false)


    // 岗位管理
    let postData = $ref([])
    const getPostList = () => {
        dataLoading = true
        // setTimeout(() => {
        //     dataLoading = false
        //     let data = { s1: '王*', s2: '男', s3: '370481********8765', s4: '汉', s5: '群众', s6: '济南', s7: '小学', s8: '1968-08-30', s9: '农村低收入人口' }
        //     postData = []
        //     for(let i = 0; i < filterParams.pageSize; i++){
        //         postData.push(data)
        //     }
        //     totalCount = 245
        // }, 300);

        getJobLibraryList(filterParams).then(res => {
            dataLoading = false
            if(res.success){
                let { records, total } = res.result

                postData = records
                totalCount = total
                console.log(resData)
            }
        })
    }

    const getList = () => {
        dataLoading = true

        if(current == 0){
            getPostList()
            return
        }

        // setTimeout(() => {
        //     dataLoading = false
        //     let data = { s1: '王*', s2: '男', s3: '370481********8765', s4: '汉', s5: '群众', s6: '济南', s7: '小学', s8: '1968-08-30', s9: '农村低收入人口' }
        //     resData = []
        //     for(let i = 0; i < filterParams.pageSize; i++){
        //         resData.push(data)
        //     }
        //     totalCount = 11623
        // }, 500);

        getJobHireList(filterParams).then(res => {
            dataLoading = false
            if(res.success){
                let { records, total } = res.result

                resData = records
                totalCount = total
            }
        })
        
    }
    getList()

    // 分页
    const handleCurrentChange = (page) => {
        filterParams.pageNo = page
        getList()
    }

    const handleSizeChange = (size) => {
        filterParams.pageSize = size
        getList()
    }

    // 搜索
    const hadleSearch = () => {
        filterParams.pageNo = 1
        getList()
    }

    // 重置
    const handleClickReset = () => {
        filterParams = {
            pageNo: 1,
            pageSize: 20
        }
    }

    // 修改岗位
    let addPost = $ref(null)
    let addPost2 = $ref(null)

    // 新增
    const addSuccess = () => {
        filterParams.pageNo = 1
        getList()
    }

    // 删除
    const clickDelete = (id) => {
        jobLibraryDelete({ id }).then(res => {
            if(res.success){
                ElMessage.success('删除成功~')
                getList()
            }else{
                ElMessage.error(res.message)
            }
        })
    }

    // 删除
    const clickDelete2 = (id) => {
        jobHireDelete({ id }).then(res => {
            if(res.success){
                ElMessage.success('删除成功~')
                getList()
            }else{
                ElMessage.error(res.message)
            }
        })
    }

</script>

<template>
    <div class="top-tabbar-wrap">
        <div 
            class="tabbar-item"
            v-for="(item, index) in tabbarData"
            :key="index"
            :class="{ active: current == index }"
            @click="current = index; handleClickReset(); getList()"
        >
            {{ item }}
        </div>
    </div>

    <div class="health-search" v-if="current == 0">
        <div class="health-search-form-warp dz-filter-wrap">
            <el-form 
                :model="filterParams" 
                :inline="true"
                :label-width="105"
                class="mt10"
            >
                <el-form-item label="所属市">
                    <el-select v-model="filterParams.sysOrgCode" placeholder=" " clearable @clear="delete filterParams.sysOrgCode">
                        <el-option
                            v-for="(item, index) in sdCityList"
                            :key="index"
                            :label="item"
                            :value="item"
                        >
                            {{ item }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="人员类别">
                    <el-select v-model="filterParams.type" placeholder=" " clearable @clear="delete filterParams.type">
                        <el-option
                            v-for="(item, index) in ['农民', '学生', '工人', '社会人士', '农村低收入人口']"
                            :key="index"
                            :label="item"
                            :value="item"
                        >
                            {{ item }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input v-model="filterParams.name"></el-input>
                </el-form-item>
                <el-form-item label="身份证号">
                    <el-input v-model="filterParams.idCard"></el-input>
                </el-form-item>

                <div class="btn-wrap flex-center">
                    <div class="btn reset" @click="handleClickReset">重置</div>
                    <div class="btn search" @click="hadleSearch">查询</div>
                </div>
            </el-form>

        </div>

        <!-- 查询结果 -->
        <div class="search-result mt10" v-loading="dataLoading">
            <div class="result-title flex-between">
                <div>
                    查询结果
                    <span class="result-count">共{{ totalCount }}条</span>
                </div>
                <div class="add-btn" @click="addPost.showAddPost({}, 'add')">新增安置人员</div>
            </div>
            
            <el-table 
                :data="postData" 
                style="width: 100%"
                height="530"
                size="large"
                class="dz-table"
                :scrollbar-always-on="true"
            >
                <el-table-column type="index" label="序号" min-width="60" align="center" />
                <el-table-column prop="name" label="姓名" min-width="80" />
                <el-table-column prop="sex" label="性别" min-width="80" align="center" />
                <el-table-column prop="idCard" label="身份证号码" min-width="180" show-overflow-tooltip />
                <el-table-column prop="nation" label="民族" min-width="80" align="center" />
                <el-table-column prop="politaues" label="政治面貌" min-width="100" align="center" />
                <el-table-column prop="city" label="所属市" min-width="100" align="center" />
                <el-table-column prop="culture" label="文化程度" min-width="100" align="center" />
                <el-table-column prop="birth" label="出生日期" min-width="120" align="center" />
                <el-table-column prop="personGenre" label="人口类型" min-width="120" />
                <el-table-column prop="v6" label="操作" min-width="200" align="center">
                    <template #default="scope">
                        <div class="table-operation flex-center">
                            <el-popconfirm title="确定要删除此条数据吗?" @confirm="clickDelete(scope.row.id)">
                                <template #reference>
                                    <span>删除</span>
                                </template>
                            </el-popconfirm>
                            <span @click="addPost.showAddPost(scope.row, 'details')">查看</span>
                            <span @click="addPost.showAddPost(scope.row, 'edit')">修改</span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination 
                class="mt20" 
                background 
                layout="total, sizes, prev, pager, next" 
                :total="totalCount" 
                :page-size="filterParams.pageSize"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
            />

        </div>
    </div>

    <div class="health-search" v-if="current == 1">
        <div class="health-search-form-warp dz-filter-wrap">
            <el-form 
                :model="filterParams" 
                :inline="true"
                :label-width="105"
                class="mt10"
            >
                <el-form-item label="所属市">
                    <el-select v-model="filterParams.sysOrgCode" placeholder=" " clearable @clear="delete filterParams.sysOrgCode">
                        <el-option
                            v-for="(item, index) in sdCityList"
                            :key="index"
                            :label="item"
                            :value="item"
                        >
                            {{ item }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="人员类别">
                    <el-select v-model="filterParams.type" placeholder=" " clearable @clear="delete filterParams.type">
                        <el-option
                            v-for="(item, index) in ['农民', '学生', '工人', '社会人士', '农村低收入人口']"
                            :key="index"
                            :label="item"
                            :value="item"
                        >
                            {{ item }}
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="姓名">
                    <el-input v-model="filterParams.name"></el-input>
                </el-form-item>
                <el-form-item label="身份证号">
                    <el-input v-model="filterParams.idCard"></el-input>
                </el-form-item>

                <div class="btn-wrap flex-center">
                    <div class="btn reset" @click="handleClickReset">重置</div>
                    <div class="btn search" @click="hadleSearch">查询</div>
                </div>
            </el-form>

        </div>

        <!-- 查询结果 -->
        <div class="search-result mt10" v-loading="dataLoading">
            <div class="result-title flex-between">
                <div>
                    查询结果
                    <span class="result-count">共{{ totalCount }}条</span>
                </div>
                <div class="add-btn" @click="addPost2.showAddPost({}, 'add')">新增登记</div>
            </div>
            <el-table 
                :data="resData" 
                style="width: 100%"
                height="530"
                size="large"
                class="dz-table"
                :scrollbar-always-on="true"
            >
                
                <el-table-column type="index" label="序号" min-width="60" align="center" />
                <el-table-column prop="name" label="姓名" min-width="80" />
                <el-table-column prop="sex" label="性别" min-width="80" align="center" />
                <el-table-column prop="idCard" label="身份证号码" min-width="180" show-overflow-tooltip />
                <el-table-column prop="nation" label="民族" min-width="80" align="center" />
                <el-table-column prop="polistatus" label="政治面貌" min-width="100" align="center" />
                <el-table-column prop="city" label="所属市" min-width="100" align="center" />
                <el-table-column prop="culture" label="文化程度" min-width="100" align="center" />
                <el-table-column prop="birth" label="出生日期" min-width="120" align="center" />
                <el-table-column prop="personGenre" label="人口类型" min-width="160" />
                <el-table-column prop="v6" label="操作" min-width="200" align="center">
                    <template #default="scope">
                        <div class="table-operation flex-center">
                            <el-popconfirm title="确定要删除此条数据吗?" @confirm="clickDelete2(scope.row.id)">
                                <template #reference>
                                    <span>删除</span>
                                </template>
                            </el-popconfirm>
                            <span @click="addPost2.showAddPost(scope.row, 'details')">查看</span>
                            <span @click="addPost2.showAddPost(scope.row, 'edit')">修改</span>
                        </div>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination 
                class="mt20" 
                background 
                layout="total, sizes, prev, pager, next" 
                :total="totalCount" 
                :page-size="filterParams.pageSize"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
            />

        </div>
    </div>

    <!-- 岗位修改 -->
    <add-post ref="addPost" @addSuccess="addSuccess"></add-post>

    <add-post2 ref="addPost2" @addSuccess="addSuccess"></add-post2>

</template>
